package cabbage.model.logic;

import java.sql.SQLException;

import cabbage.database.idao.ILiveCollectibleDAO;
import cabbage.database.idao.ILocationDAO;
import cabbage.database.operations.LiveCollectibleDAO;
import cabbage.database.operations.LocationDAO;
import cabbage.logic.i.ILiveCollectibleCreator;

public class LiveCollectibleCreator implements ILiveCollectibleCreator {
	
	private ILiveCollectibleDAO liveCollectible;
	private ILocationDAO location;
	
	public LiveCollectibleCreator() {
		
	}
	
	@Override
	public int createLiveCollectible(int collectiblemasterid, int locationid) throws SQLException {
		if (collectiblemasterid == 0) return 0;
		if (locationid == 0) return 0;
		if (!validateLocationID(locationid)) return 0;
		if (!validateCollectibleMasterID(collectiblemasterid)) return 0;
		if (checkForDuplicate(collectiblemasterid,locationid)) return 0;
		liveCollectible = new LiveCollectibleDAO();
		return liveCollectible.createLiveCollectible(collectiblemasterid, locationid);
	}
	
	private boolean validateCollectibleMasterID(int id) throws SQLException {
		liveCollectible = new LiveCollectibleDAO();
		return liveCollectible.checkLiveCollectibleidExists(id);
	}
	
	private boolean validateLocationID(int id) throws SQLException {
		location = new LocationDAO();
		return location.checkLocationidExists(id);
	}
	
	private boolean checkForDuplicate(int collectiblemasterid, int locationid) throws SQLException {
		liveCollectible = new LiveCollectibleDAO();
		return liveCollectible.checkLiveCollectibleExistsAtLocation(collectiblemasterid, locationid);
	}
	
	

}
